package com.gotop.system.wrapper;

import com.gotop.common.constant.CommonConstant;
import com.gotop.common.node.ForestNodeMerger;
import com.gotop.common.node.INode;
import com.gotop.common.utils.ApplicationStringUtils;
import com.gotop.common.utils.support.BeanUtil;
import com.gotop.entity.system.Role;
import com.gotop.system.service.system.IRoleService;
import com.gotop.vo.system.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author superfish
 */
public class RoleWrapper extends BaseEntityWrapper<Role, RoleVO> {

    @Autowired
    private static IRoleService roleService;

    static {
        roleService = ApplicationStringUtils.getBean(IRoleService.class);
    }

    public static RoleWrapper build() {
        return new RoleWrapper();
    }

    @Override
    public RoleVO entityVO(Role role) {
        RoleVO roleVO = BeanUtil.copy(role, RoleVO.class);
        if (role.getParentId().equals(CommonConstant.TOP_PARENT_ID)) {
            roleVO.setParentName(CommonConstant.TOP_PARENT_NAME);
        } else {
            Role parent = roleService.getById(role.getParentId());
            roleVO.setParentName(parent.getRoleName());
        }
        return roleVO;
    }

    public List<INode> listNodeVO(List<Role> list) {
        List<INode> collect = list.stream().map(this::entityVO).collect(Collectors.toList());
        return ForestNodeMerger.merge(collect);
    }

}
